package com.lyshop.jinxiang.activitys;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lyshop.jinxiang.R;
import com.lyshop.jinxiang.adapters.RedBagRecordAdapter;
import com.lyshop.jinxiang.base.BaseActivity;
import com.lyshop.jinxiang.bean.RedBagBean;
import com.lyshop.jinxiang.net.ActionCallbackListener;
import com.lyshop.jinxiang.utils.AppUtils;
import com.lyshop.jinxiang.utils.FastClickUtil;
import com.lyshop.jinxiang.utils.ToastUtil;
import com.lyshop.jinxiang.view.CommonRuleDialog;

import java.util.ArrayList;
import java.util.List;

public class RedBagRainActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_progress;//20 120 210
    private ImageView iv_one, iv_two, iv_three, iv_back;
    private TextView tv_rule, tv_time_one, tv_result_one, tv_time_two, tv_result_two, tv_time_three, tv_result_three, tv_draw;
    private RecyclerView rv_record;
    private RedBagRecordAdapter redBagRecordAdapter;
    private List<RedBagBean> mRedBagBeanList = new ArrayList<>();
    private int mEnable;//0未在时间内 1已领取 2可领取
    private String mRule = "1.交易日的 12:00   16:00   20:00  三个准点发放50000积分共计  100个  抢完为止\n" +
            "2.参与条件:完成首充并有现金下单的用户,每个用户每场只能领取一次\n" +
            "3、积分可在积分商城兑换代金券，100积分等于1元代金券，等级越高可享受更多兑换折扣。\n" +
            "4、如使用非正常手段套取活动奖励，一经核实，奖励不予发放，并进行封号处理";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_red_bag_rain;
    }

    @Override
    protected void findViews() {
        iv_back = $(R.id.iv_back);
        tv_rule = $(R.id.tv_rule);
        tv_draw = $(R.id.tv_draw);
        iv_progress = $(R.id.iv_progress);
        iv_one = $(R.id.iv_one);
        tv_time_one = $(R.id.tv_time_one);
        tv_result_one = $(R.id.tv_result_one);

        iv_two = $(R.id.iv_two);
        tv_time_two = $(R.id.tv_time_two);
        tv_result_two = $(R.id.tv_result_two);

        iv_three = $(R.id.iv_three);
        tv_time_three = $(R.id.tv_time_three);
        tv_result_three = $(R.id.tv_result_three);
        rv_record = $(R.id.rv_record);

        rv_record.setLayoutManager(new LinearLayoutManager(mContext));
        redBagRecordAdapter = new RedBagRecordAdapter();
        rv_record.setAdapter(redBagRecordAdapter);
    }

    @Override
    protected void setListeners() {
        tv_draw.setOnClickListener(this);
        tv_rule.setOnClickListener(this);
        iv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == tv_draw){
            if (FastClickUtil.isFastClick()) return;
            if (mEnable == 0){
                ToastUtil.showToast(mContext, "未在活动时间范围内，请退出重新刷新！");
                return;
            }
            if (mEnable == 1){
                ToastUtil.showToast(mContext, "已抢到红包，请勿重复领取！");
                return;
            }
            loading();
            mAppAction.drawRedBag(new ActionCallbackListener<Void>() {
                @Override
                public void onSuccess(Void data) {
                    cancelLoading();
                    initData();
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    cancelLoading();
                    ToastUtil.showToast(mContext, message);
                }
            });
        } else if (v == tv_rule){
            CommonRuleDialog dialog = new CommonRuleDialog(mContext, mRule);
            if (!dialog.isShowing()) dialog.show();
        } else if (v == iv_back){
            backActivity();
        }
    }

    private void initData() {
        mAppAction.getRedBagList(new ActionCallbackListener<List<RedBagBean>>() {
            @Override
            public void onSuccess(List<RedBagBean> data) {
                if (data.size() == 0) return;
                mRedBagBeanList = data;
                int currentActivity = -1;
                int finishActivity = -1;
                int nextSum = 0;
                int finishSum = 0;
                for (int i = 0; i < data.size(); i++) {
                    setTimeView(i, data.get(i));
                    if (data.get(i).status == 0){
                        nextSum++;
                    }else if (data.get(i).status == 1){
                        currentActivity = i;
                    }else if (data.get(i).status == 2){
                        finishActivity = i;
                        finishSum++;
                    }
                }
                setProgressView(currentActivity, finishActivity, nextSum, finishSum);
                redBagRecordAdapter.setRedBagBeanList(data);
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    private void setProgressView(int currentActivity, int finishActivity, int nextSum, int finishSum) {
        if (currentActivity == -1){
            if (nextSum == 3){
                //全未开始
                iv_progress.getLayoutParams().width = 0;
            }else if (finishSum == 3){
                //全结束
                iv_progress.getLayoutParams().width = AppUtils.dp2px(211);
            }else {
                if (finishActivity == 0){
                    iv_progress.getLayoutParams().width = AppUtils.dp2px(20);
                }else if (finishActivity == 1){
                    iv_progress.getLayoutParams().width = AppUtils.dp2px(120);
                }
            }
            mEnable = 0;
        }else {
            if (currentActivity == 0){
                iv_progress.getLayoutParams().width = AppUtils.dp2px(20);
            }else if (currentActivity == 1){
                iv_progress.getLayoutParams().width = AppUtils.dp2px(120);
            }else if (currentActivity == 2){
                iv_progress.getLayoutParams().width = AppUtils.dp2px(210);
            }
            if (mRedBagBeanList.get(currentActivity).integral != null){
                mEnable = 1;
            }else {
                mEnable = 2;
            }
        }
        iv_progress.requestLayout();
    }

    private void setTimeView(int index, RedBagBean redBagBean) {
        if (index == 0){
            if (redBagBean.status == 0){
                iv_one.setImageResource(R.drawable.ic_red_bag_false);
                tv_result_one.setText("未开始");
            }else if (redBagBean.status == 1){
                iv_one.setImageResource(R.drawable.ic_red_bag_true);
                tv_result_one.setText("正在进行中");
            }else if (redBagBean.status == 2){
                iv_one.setImageResource(R.drawable.ic_red_bag_true);
                tv_result_one.setText("已结束");
            }
            tv_time_one.setText(redBagBean.startTime);
        }else if (index == 1){
            if (redBagBean.status == 0){
                iv_two.setImageResource(R.drawable.ic_red_bag_false);
                tv_result_two.setText("未开始");
            }else if (redBagBean.status == 1){
                iv_two.setImageResource(R.drawable.ic_red_bag_true);
                tv_result_two.setText("正在进行中");
            }else if (redBagBean.status == 2){
                iv_two.setImageResource(R.drawable.ic_red_bag_true);
                tv_result_two.setText("已结束");
            }
            tv_time_two.setText(redBagBean.startTime);
        }else {
            if (redBagBean.status == 0){
                iv_three.setImageResource(R.drawable.ic_red_bag_false);
                tv_result_three.setText("未开始");
            }else if (redBagBean.status == 1){
                iv_three.setImageResource(R.drawable.ic_red_bag_true);
                tv_result_three.setText("正在进行中");
            }else if (redBagBean.status == 2){
                iv_three.setImageResource(R.drawable.ic_red_bag_true);
                tv_result_three.setText("已结束");
            }
            tv_time_three.setText(redBagBean.startTime);
        }
    }

}